package gui.wizard;

import generated.business.avatar.AvatarServicePackage.Gender;
import generated.business.avatar.AvatarServicePackage.Mood;
import generated.business.avatar.AvatarServicePackage.Size;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.PlatformUI;

import vworld.Client;

public class StartWizard extends Wizard {

	public StartWizard() {

		addPage(new WelcomePage());
		addPage(new CreationAvatarPage());

		setWindowTitle("Welcome to VWorld");
	}

	/**
	 * Called when user clicks Finish
	 * 
	 * @return boolean
	 */
	public boolean performFinish() {
		CreationAvatarPage creationAvatarPage = (CreationAvatarPage) getPage("CreationAvatarPage");

		String name = creationAvatarPage.getNameText().getText();
		Mood mood = null;
		if (creationAvatarPage.getMoodCombo().getText().equals("happy")) {
			mood = Mood.from_int(0);
		} else if (creationAvatarPage.getMoodCombo().getText().equals(
				"laughing")) {
			mood = Mood.from_int(1);
		} else if (creationAvatarPage.getMoodCombo().getText().equals("sad")) {
			mood = Mood.from_int(2);
		} else if (creationAvatarPage.getMoodCombo().getText()
				.equals("worried")) {
			mood = Mood.from_int(3);
		} else if (creationAvatarPage.getMoodCombo().getText().equals("scared")) {
			mood = Mood.from_int(4);
		}
		Gender gender = null;
		if (creationAvatarPage.getGenderCombo().getText().equals("male")) {
			gender = Gender.from_int(0);
		} else if (creationAvatarPage.getGenderCombo().getText().equals(
				"female")) {
			gender = Gender.from_int(1);
		}
		Size size = null;
		if (creationAvatarPage.getSizeCombo().getText().equals("dwarf")) {
			size = Size.from_int(0);
		} else if (creationAvatarPage.getSizeCombo().getText().equals("small")) {
			size = Size.from_int(1);
		} else if (creationAvatarPage.getSizeCombo().getText()
				.equals("average")) {
			size = Size.from_int(2);
		} else if (creationAvatarPage.getSizeCombo().getText().equals("tall")) {
			size = Size.from_int(3);
		} else if (creationAvatarPage.getSizeCombo().getText().equals("giant")) {
			size = Size.from_int(4);
		}

		if (!Client.AVATAR_SERVICE.existAvatar(name)) {
			Client.CONNECTED_USER.avatar = Client.AVATAR_SERVICE.createAvatar(
					Client.CONNECTED_USER, name, mood, gender, size);
			return true;
		} else {
			MessageDialog
					.openInformation(this.getShell(),
							"Avatar alreday existing",
							"An avatar with this name already exists. Please choose a different one.");
			return false;
		}

	}

	@Override
	public boolean performCancel() {
		PlatformUI.getWorkbench().restart();
		return super.performCancel();
	}
}
